/**
* 新增供货商维权处理弹框：仅退款 Add by 2015-1-14
*/
<template>
    <kdx-modal-frame
            v-model="value"
            title="维权处理"
            :width="700"
            @on-cancel="handleCancel"
            @on-ok="handleOk"
    >
      <Form ref="form" :label-width="120" class="order-refund-detail-refund-modal">
        <FormItem label="维权类型：" class="r-form-item-text">
          <kdx-tag-label type="danger" size="small">仅退款</kdx-tag-label>
          <!--  供货商维权状态-->
          <p v-if="isJST && data.refund_info.supplier_status" class="alert-blue">
            供货商维权状态：{{ data.refund_info.supplier_status }}
          </p>
        </FormItem>
        <FormItem label="处理方式：" class="r-form-item-checkbox">
          <RadioGroup v-model="model.type">
            <Radio label="0">自动退款</Radio>
            <Radio label="1">手动退款</Radio>
            <Radio label="2">驳回申请</Radio>
          </RadioGroup>
          <kdx-hint-alert :show-icon="false" v-show="model.type === '0'">
            <p>微信支付方式：会返回到相应的支付渠道(如零钱或银行卡)</p>
            <p>其他支付方式：会返回到微信钱包(需商户平台余额充足)</p>
            <p>如有余额抵扣：会返回金额到商城用户余额</p>
            <p>如有积分抵扣：申请维权的订单抵扣积分会返回到用户积分中</p>
          </kdx-hint-alert>
          <kdx-hint-alert :show-icon="false" v-show="model.type === '1'">
            <p>手动退款：订单会完成退款处理，您用其他方式进行退款</p>
          </kdx-hint-alert>
        </FormItem>
        <!-- <FormItem label="返回方式：" v-show="false&&model.type === '1'">
          <Input v-model="model.style" class="width-430" placeholder="请手动输入返回方式" />
        </FormItem> -->
        <!--      <FormItem label="留言：" v-show="false&&(model.type === '0' || model.type === '1')">-->
        <!--        <Input-->
        <!--          v-model="model.message"-->
        <!--          type="textarea"-->
        <!--          autosize-->
        <!--          :maxlength="200"-->
        <!--          class="width-430"-->
        <!--          placeholder="请输入"-->
        <!--        />-->
        <!--      </FormItem>-->
        <FormItem label="驳回原因：" v-show="model.type === '2'">
          <Input
              v-model="model.reason"
              type="textarea"
              :maxlength="200"
              class="width-430"
              placeholder="请输入"
          />
        </FormItem>
<!--          聚水潭商品订单：已发货的自动退款、手动退款，需要创建供货商维权售后单 -->
            <div class="content" v-show="isJST && data.status>=11 && model.type != '2'">
              <div class="title">
                供货商维权售后处理
                <kdx-tag-label type="danger" size="small">请提前确认申请金额</kdx-tag-label>
              </div>
              <FormItem label="申请退款金额：">
                <kdx-rr-input v-model="model.supplier_refund_price" number :fixed="2" :minValue="0" :maxValue="9999999.99" class="width-160"
                              @on-blur="changeSupplierRefund">
                  <span slot="append">元</span>
                </kdx-rr-input>
              </FormItem>
              <div class="label" v-for="(item, index) in data.orderGoods" :key="index">
                <kdx-tag-label type="danger" size="small">参考</kdx-tag-label>
                商品：{{item.title}}，数量：{{item.total}}，成本小计：
                <span class="price"> ￥{{item.cost_price}}</span>
              </div>
            </div>

        </Form>
    </kdx-modal-frame>
</template>

<script>

    export default {
        name: 'RefundModalNew',
        props: {
            visible: {
                type: Number,
                default: 0
            },
            data: {
                type: Object,
                default: () => {
                }
            }
        },
        data() {
            return {
                value: false,
                isCan: false, // 添加锁
                model: {
                    type: '0',
                    // message: '',
                    reason: '',
                    supplier_refund_price: 0
                }
            }
        },
        watch: {
            visible() {
                this.value = true
            }
        },
        methods: {
            isJST() {
              return (this.data.refund_info && this.data.refund_info.supplier == 'jushuitan') ? true : false;
            },
            // 供货商维权售后处理金额
            changeSupplierRefund() {
              if (this.model.supplier_refund_price <= 0) {
                return this.$Message.error('申请退款金额不可为0');
              }
              this.$nextTick(() => {
                if (parseFloat(this.model.supplier_refund_price) && parseFloat(this.model.supplier_refund_price) > 0) {
                  this.model.supplier_refund_price = parseFloat(this.model.supplier_refund_price).toFixed(2);
                } else {
                  this.model.supplier_refund_price = '0.00';
                  this.$set(this.model, 'supplier_refund_price', '0.00');
                }
              })
            },
            handleCancel() {
                this.value = false
            },
            handleOk() {
                if (this.isCan) {
                    return;
                }
                let type = this.model.type
                let {
                    id,
                    refund_type,
                    refund_info: {order_goods_id}
                } = this.data

                let data = {
                    order_id: id
                }

                if (refund_type == '2') {
                    data.order_goods_id = order_goods_id
                }
                this.isCan = true;

               data = this.handleSupplierRefundGood(type, data)

                if (type === '2') {// 驳回申请
                    if (this.model.reason) {
                        data.reject_reason = this.model.reason
                    }
                    this.submit('refundRejectOrder', data)
                } else if (type === '1') {// 手动退款
                    this.submit('manualRefund', data)
                } else {// 自动退款
                    this.submit('autoRefund', data)
                }
            },
            submit(type, data) {
                this.$api.orderApi[type](data)
                    .then(res => {
                        if (res.error === 0) {
                            this.$Message.success('操作成功');
                            this.$parent.$parent.getData()
                        } else {
                          this.$Message.error('处理申请失败')
                        }
                    })
                    .finally(() => {
                        this.value = false;
                        this.isCan = false;
                    })
            },
          // 供货商维权售后处理数据
          handleSupplierRefundGood(type, data) {
            // 组合供货商维权售后处理数据
            if (this.data.refund_info.supplier && this.data.refund_info.supplier == 'jushuitan' && type != '2') {
              let supplier_refund_info = {
                order_id: parseInt(this.data.refund_info.order_id),
                refund_type: this.data.refund_info.refund_type,
                refund_no: this.data.refund_info.refund_no,
                order_goods_id: parseInt(this.data.refund_info.order_goods_id),
                price: this.model.supplier_refund_price,
                reason: this.data.refund_info.content,
                supplier: this.data.refund_info.supplier,
                supplierType: this.data.refund_info.supplierType ? this.data.refund_info.supplierType : '',
              }
              data.supplier_refund_info = supplier_refund_info;
            }
            return  data;
          }
        }
    }
</script>

<style scoped lang="scss">
    .order-refund-detail-refund-modal {
        padding: 30px 20px 20px 0;

        .ivu-form-item {
            margin-bottom: 20px;
        }

        .hint-alert-component {
            margin-top: 10px;

            p {
                @include font-12-24;
            }
        }

      .content {
        .title {
          @include font-16-22-bold;
          color: $text-first;
          padding: 10px;
        }
        .text {
          @include font-14-20-bold;
          padding: 10px 10px;
        }
        > .label {
          margin-left: 10px;

          > .price {
            @include font-14-20-bold;
            color: $danger-color;
          }
        }
        .ivu-form-item {
          margin-bottom: 9px !important;
        }
      }
    }
</style>
